<template>
	<view>
		<Cell title="设置" isLink @handleCellClick="handleSet()" />
		
		<Cell title="意见反馈" isLink @handleCellClick="handleFeedback()" class="mar-t-15" />
		<Cell title="关于我们" isLink @handleCellClick="handleAbout()" style="margin-top: 1px;" />
		
		<Cell title="清除缓存" :rightContent="plusCacheSize" class="mar-t-15" @handleCellClick="handleClearStorage()" />
		<Cell title="当前版本" :rightContent="version" style="margin-top: 1px;" />
		
		<view
			class="pos-f bg-white flex align-center justify-center h-px-50 border-rad-3"
			hover-class="hoverClass"
			style="left: 15px; right: 15px; bottom: 100px;"
			@click="handleLogout()"
		>
			<text class="font-14">退出登录</text>
		</view>
	</view>
</template>

<script>
	import Cell from '@/components/Cell/index'
	import Utils from '@/utils/utils'
	
	import { mapState } from 'vuex'
	import { getPlusCacheSize, checkLogin } from '@/utils/lib'
	
	export default {
		components: {
			Cell
		},
		
		computed: {
			...mapState({
				version: state => state.user.version
			})
		},
		
		data() {
			return {
				// 应用缓存大小
				plusCacheSize: getPlusCacheSize()
			}
		},
		
		onShow() {
			checkLogin()
		},
		
		methods: {
			// 点击设置的回调
			handleSet() {
				uni.navigateTo({
					url: '/pages/setConfig/setConfig'
				})
			},
			
			// 退出登录
			handleLogout() {
				Utils.modal('提示', '您确定退出登录吗？', (res) => {
					if (res.confirm) {
						this.$store.dispatch('user/logout').then(res => {
							Utils.popUpMsg('success', '退出成功！', 500)
							setTimeout(() => {
								checkLogin()
							})
						})
					}
				})
			},
			
			// 清除本地缓存
			handleClearStorage() {
				// 先将用户信息备份，防止被清理掉
				let isLogin = JSON.parse(JSON.stringify(this.$store.state.user.isLogin))
				let userInfo = JSON.parse(JSON.stringify(this.$store.state.user.userInfo))
				let username = JSON.parse(JSON.stringify(this.$store.state.user.username))
				let avatarUrl = JSON.parse(JSON.stringify(this.$store.state.user.avatarUrl))
				let token = JSON.parse(JSON.stringify(this.$store.state.user.token))
				let menuPermissions = JSON.parse(JSON.stringify(this.$store.state.user.menuPermissions))
				
				try {
					uni.clearStorageSync()
					Utils.popUpMsg('success', '清理成功!', 1000)
				} catch (e) {
					// error
					console.log('清理缓存失败！')
				}
				
				this.$store.commit('user/setLoginState', isLogin) // 设置登录状态（登录成功）
				this.$store.commit('user/setToken', token) // 设置 token
				this.$store.commit('user/setUserInfo', userInfo) // 将登录成功后的用户信息保存下来，方便存取
				this.$store.commit('user/setUserName', username) // 将登录成功的用户昵称存下来，方便展示
				this.$store.commit('user/setAvatar', avatarUrl) // 将登录成功的用户头像存下来，方便展示
				this.$store.commit('user/setMenuPermissions', menuPermissions) // 将登录成功的用户菜单权限拿到，方便后面菜单做逻辑
				
				// 重新获取缓存大小和检测登录
				this.plusCacheSize = getPlusCacheSize()
				checkLogin()
			},
			
			// 意见反馈
			handleFeedback() {
				uni.navigateTo({
					url: '/pages/feedBack/feedBack'
				})
			},
			
			// 关于我们
			handleAbout() {
				uni.navigateTo({
					url: '/pages/about/about'
				})
			},
		}
	}
</script>

<style>

</style>
